<script setup lang='ts'>
  import { onMounted, reactive, ref, shallowRef } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { Download } from '@/utils'
  import { ElMessageBox } from 'element-plus';
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import activityForm from './activityForm.vue';
  import xmImage from '@/components/xmImage/index.vue';
  import { useRouter } from 'vue-router'
  const $router = useRouter()
  const activityFormRef = ref(),
  componentRef = shallowRef()
  const action = reactive({
    total:0,
    dialogTableVisible:false,
    activityID:0,
    list:[] as any[],
    timeObj:{
      playTime:0,
      endTime:0,
    }
  })
  const typeOptions = [
    {type:1,text:'用户ID'},
    {type:2,text:'用户手机号'},
  ]
  const listQuery = reactive({
    page:1,
    limit:20,
    name:''
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleForm = (row:any = {})=>{
    activityFormRef.value.openDialog({
      ...row,
      typeOptions
    })
  }
  const openComponents = (row:any = {},path:any)=>{
    const { id } = row
    $router.push({
      path,
      query: {
        activityID:id
      }
    })
    // action.activityID = id
    // componentRef.value = comRef
    // action.dialogTableVisible = true
  }
  const handleDelete = (row:any = {})=>{
    const { id=0 } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm("确定删除此活动配置吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.del_act_mission({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:start_time, endTime:end_time } = action.timeObj
    const res = await gamesApi.act_mission_list({
      start_time,
      end_time,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动名称：</div>
          <el-input type="text" v-model="listQuery.name" placeholder="请输入活动名称" />
        </div>
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handleForm">创建活动</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column fixed="left" width="100" prop="id" label="活动ID" />
      <el-table-column fixed="left" min-width="180" show-overflow-tooltip prop="ac_name" label="活动名称" />
      <el-table-column width="160" prop="uuid" label="任务图标">
        <template #default="scoped">
          <xmImage :src="scoped.row.icon" :list="[scoped.row.icon]" />
        </template>
      </el-table-column>
      <el-table-column width="180" prop="start_time" label="开始时间" />
      <el-table-column width="180" prop="end_time" label="结束时间" />
      <el-table-column width="120" prop="days" label="个人持续天数" />
      <el-table-column width="120" prop="type" label="回归类型">
        <template #default="scoped">
          {{ typeOptions.find(res=>res.type == scoped.row.type)?.text || scoped.row.type }}
        </template>
      </el-table-column>
      <el-table-column width="120" label="已上传文件">
        <template #default="scoped">
          <el-button v-if="scoped.row.execl_url" @click="Download(scoped.row.execl_url)" type="primary">下载</el-button>
        </template>
      </el-table-column>
      <el-table-column width="180" show-overflow-tooltip prop="user_ids" label="人员名单" />
      <el-table-column width="180" show-overflow-tooltip prop="phones" label="回归用户手机号" />
      <el-table-column width="290" fixed="right" label="操作">
        <template #default="scoped">
          <el-row>
            <el-col :span="24" class="xm-mb10">
              <el-button type="primary" @click="handleForm(scoped.row)">编辑活动</el-button>
              <el-button type="primary" @click="openComponents(scoped.row,'/lossRecall/signinTask')">签到配置</el-button>
            </el-col>
            <el-col :span="24">
              <el-button type="primary" @click="openComponents(scoped.row,'/lossRecall/dayTask')">每日任务</el-button>
              <el-button type="primary" @click="openComponents(scoped.row,'/lossRecall/totalTask')">累计任务</el-button>
              <el-button type="danger" @click="handleDelete(scoped.row)">删除</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <activityForm ref="activityFormRef" @update-list="getList" />
    <!-- <el-dialog
      top="5vh"
      class="table-dialog"
      v-model="action.dialogTableVisible"
      width="80%"
      title="">
      <component :is="componentRef" :activityID="action.activityID"></component>
    </el-dialog> -->
  </div>
  
</template>
<!-- <style lang="scss" scoped>
  :deep(.table-dialog) {
    .el-dialog__body {
      padding: 0 !important;
      height: max-content;
    }
  }
</style> -->